package cn.ococci.dm.Main.Fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.List;

import cn.ococci.dm.Base.BaseApp;
import cn.ococci.dm.Base.BaseFragment;
import cn.ococci.dm.Main.Adapter.Base.AbNormalAdapter;
import cn.ococci.dm.Main.Adapter.Base.BaseViewHolder;
import cn.ococci.dm.Main.DoorBiz;
import cn.ococci.dm.Main.Model.UnitModel;
import cn.ococci.dm.Main.Tools.DbTool;
import cn.ococci.dm.Main.Tools.FilterTool;
import cn.ococci.dm.Main.Tools.StringTool;
import cn.ococci.dm.Main.Tools.ThreadTool;
import cn.ococci.dm.Network.HttpCallback;
import cn.ococci_dm.R;

/**
 * Created by cyan on 15/12/29.
 *
 * 选择楼栋单元操作界面（返回跳转到上层。并且返回用用户选中的数据）
 *
 */
public class ChoseFloorFragment extends BaseFragment {

    private ListView floorListLv;
    private Button addFloorBtn;

    private List<UnitModel> showFloorList;  // 显示的数据
    private List<UnitModel> selectedFloorList;  // 选中的数据

    private AbNormalAdapter abNormalAdapter;

    @Override
    protected int getResId() {
        return R.layout.fragment_modify_select_unit;
    }

    @Override
    protected void initView(View parent) {

        floorListLv = (ListView) parent.findViewById(R.id.floorListLv);
        addFloorBtn = (Button) parent.findViewById(R.id.addFloorBtn);

    }


    @Override
    protected void initData() {

        showFloorList  = DbTool.findAllUnitModel();

        if (!StringTool.hasData(showFloorList)){

           DoorBiz.getFloorList(new HttpCallback<UnitModel>() {
               @Override
               public void onSuccessList(List<UnitModel> list) {
                   showFloorList = list;
                   ThreadTool.runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           initAdapter();
                       }
                   });
               }
           });
        }

    }


    @Override
    protected void initAdapter() {

        if (StringTool.hasData(selectedFloorList)){
            FilterTool.matchUnitListData(selectedFloorList,showFloorList);
        }

        abNormalAdapter = new AbNormalAdapter<UnitModel>(getActivity(), R.layout.fragment_select_unit_lv_item, showFloorList) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, final UnitModel unit, final int pos) {
                baseViewHolder.setTextView(R.id.unitNameTv, unit.getUnitName());
                baseViewHolder.setImageView(R.id.selectedImage,unit.isSelected()? R.mipmap.selected_img:R.mipmap.disselect_image);
            }
        };

        floorListLv.setAdapter(abNormalAdapter);

        floorListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                FilterTool.handlerSelectedFloorList(showFloorList,position,selectedFloorList);
                abNormalAdapter.notifyDataSetChanged();
            }
        });

    }


    @Override
    protected void initListener() {

        addFloorBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
    }

    @Override
    public String getCaption() {
        return BaseApp.findResIdDecoderString(R.string.select_unit_title);
    }

    public ChoseFloorFragment setYetSelectList(List<UnitModel> list){

        selectedFloorList = list;
        return this;

    }

}
